package com.doit.day01;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;

public class _03_scan数据1 {
    public static void main(String[] args) throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum","linux01:2181");

        Connection conn = ConnectionFactory.createConnection(conf);

        Table table = conn.getTable(TableName.valueOf("doit:student"));

        Scan scan = new Scan();
//        scan.withStartRow("000005".getBytes(StandardCharsets.UTF_8));
//        scan.withStopRow("000006".getBytes(StandardCharsets.UTF_8));
//        scan.addColumn("base_info".getBytes(StandardCharsets.UTF_8),"name".getBytes(StandardCharsets.UTF_8));
//        scan.addColumn("base_info".getBytes(StandardCharsets.UTF_8),"gender".getBytes(StandardCharsets.UTF_8));
//        scan.addColumn("extra_info".getBytes(StandardCharsets.UTF_8),"address".getBytes(StandardCharsets.UTF_8));
//        new SubstringComparator()

        ArrayList<Filter> filters = new ArrayList<>();
        RowFilter rowFilter = new RowFilter(CompareOperator.LESS_OR_EQUAL,new BinaryComparator("000002".getBytes(StandardCharsets.UTF_8)));

        RowFilter rowFilter1 = new RowFilter(CompareOperator.GREATER_OR_EQUAL,new BinaryComparator("000006".getBytes(StandardCharsets.UTF_8)));

        filters.add(rowFilter);
        filters.add(rowFilter1);

        FilterListWithOR filterListWithOR = new FilterListWithOR(filters);

        scan.setFilter(filterListWithOR);

        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        while (iterator.hasNext()){
            Result next = iterator.next();
            while (next.advance()){
                Cell current = next.current();
                String family = new String(CellUtil.cloneFamily(current));
                String qualifier = new String(CellUtil.cloneQualifier(current));
                String value = new String(CellUtil.cloneValue(current));
                String rowKey = new String(CellUtil.cloneRow(current));
                System.out.println(rowKey+"=>"+family+"=>"+qualifier+"=>"+value);
            }
        }

    }
}
